package com.hm.ibsm.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.ServerResponse;
import com.hm.ibsm.manage.service.RoleService;
import com.hm.ibsm.manage.vo.role.RoleVo;
import com.hm.ibsm.util.ControllerResult;
import com.hm.ibsm.util.R.API;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年2月10日 上午1:43:13
 * @version 1.0
 * @describe 角色管理
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(API.MODULE_NAME + "/role")
public class ApiRole {

	private static final String MODULE_TAG_NAME = "角色管理";

	@Autowired
	private RoleService roleService;

	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "添加角色", httpMethod = "POST", notes = "添加角色,返回是否成功!")
	public ResponseEntity<ServerResponse<Boolean>> addRole(
			@ApiParam(required = true, name = "body", value = "添加角色数据结构") @RequestBody(required = true) RoleVo role) {
		try {
			roleService.addRole(role);
			return ControllerResult.success(true);
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{roleId}", method = RequestMethod.PUT)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "修改角色", httpMethod = "PUT", notes = "修改角色,返回是否成功!")
	public ResponseEntity<ServerResponse<Boolean>> updateRole(
			@ApiParam(required = true, name = "roleId", value = "角色ID") @PathVariable("roleId") String roleId,
			@ApiParam(required = true, name = "body", value = "修改角色数据结构") @RequestBody(required = true) RoleVo role) {
		try {
			roleService.updateRole(roleId, role);
			return ControllerResult.success(true);
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{roleId}", method = RequestMethod.DELETE)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "删除角色", httpMethod = "DELETE", notes = "删除角色,返回是否成功!")
	public ResponseEntity<ServerResponse<Boolean>> deleteRole(
			@ApiParam(required = true, name = "roleId", value = "角色ID") @PathVariable("roleId") String roleId) {
		try {
			roleService.deleteRole(roleId);
			return ControllerResult.success(true);
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{roleId}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "获取角色详细信息", httpMethod = "GET", notes = "获取角色详细信息")
	public ResponseEntity<ServerResponse<RoleVo>> getUserDetail(
			@ApiParam(required = true, name = "roleId", value = "角色ID") @PathVariable("roleId") String roleId) {
		try {
			return ControllerResult.success(roleService.getRoleById(roleId));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}

	@RequestMapping(path = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = MODULE_TAG_NAME, value = "分页查询角色信息", httpMethod = "GET", notes = "分页查询角色信息")
	public ResponseEntity<ServerResponse<PageInfo<RoleVo>>> query(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = false, name = "roleName", value = "角色名称") @RequestParam(name = "roleName", required = false) String roleName) {
		try {

			return ControllerResult.success(roleService.query(new PageInfo.PageParam(pageNumber, pageSize), roleName));
		} catch (Exception e) {
			log.error(MODULE_TAG_NAME, e);
			return ControllerResult.failed(e);
		}
	}
}
